#! /usr/bin/env python3

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img = cv.imread("../pic/形态学运算.png", 0)
kernel = np.ones((5, 5), np.uint8)

# # 腐蚀
# opt = cv.erode(img, kernel, iterations=1)

# # 膨胀
# opt = cv.dilate(img, kernel, iterations=1)

# # 开运算
# opt = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)

# # 闭运算
# opt = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)

# # 顶帽
# opt = cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel)

# 黑帽
opt = cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel)

# # 结构元素
# str_opt = cv.getStructuringElement(cv.MORPH_RECT, (5, 5))
# print(str_opt)

plt.subplot(1, 2, 1), plt.imshow(img, "gray"), plt.title("Origin")
plt.subplot(1, 2, 2), plt.imshow(opt, "gray"), plt.title("Opt")
plt.show()
